import axios from 'axios'

let error = console.error

const api = '/accounts'

export function save (params, cb, fcb = () => error('save')) {
  axios.post(api, params).then(res => {
    cb(res.data)
  }).catch(error => {
    fcb(error)
  })
}

export function modifyEnable (params, cb, fcb = () => error('modifyEnable')) {
  // TODO
  axios.put(api, params).then(res => {
    cb(res.data)
  }).catch(error => {
    fcb(error)
  })
}

export function resetPwd (id, cb, fcb = () => error('resetPwd')) {
  axios.put(`${api}/${id}/reset-pwd`).then(res => {
    cb(res.data)
  }).catch(error => {
    fcb(error)
  })
}

export function changeCurrentUserPwd (param, cb, fcb = () => error('changeCurrentUserPwd')) {
  axios.put(`${api}/change-current-user-pwd`, param).then(res => {
    cb(res.data)
  }).catch(error => {
    fcb(error)
  })
}

export function findAll (params, cb, fcb = () => error('findAll')) {
  axios.get(api, {params}).then(res => {
    let data = res.data
    cb(data)
  }).catch(error => {
    fcb(error)
  })
}

export function getCurrentUser (cb, fcb = () => error('getCurrentUser')) {
  axios.get(`/current/user`).then(res => {
    let data = res.data
    cb(data)
  }).catch(error => {
    fcb(error)
  })
}
